package com.demo.utils;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

public class ConnUtil {
    private static String DRIVAER_CLASS_NAME;
    private static String URL;
    private static String USER;
    private static String PASSWORD;
    static {
        try {

            Properties properties = new Properties();
            InputStream is = ConnUtil.class.getClassLoader().getResourceAsStream("jdbc.properties");
            properties.load(is);
            DRIVAER_CLASS_NAME = properties.getProperty("driverClassName");
            URL = properties.getProperty("url");
            USER = properties.getProperty("user");
            PASSWORD = properties.getProperty("password");
            Class.forName(DRIVAER_CLASS_NAME);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
    private ConnUtil() {
    }

    public static Connection getConn() {
        Connection conn = null;
        try {
            conn = DriverManager.getConnection(URL, USER, PASSWORD);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return conn;
    }
}
